package com.ahui.mallchat.common.user.controller;


import com.ahui.mallchat.common.common.domain.vo.resp.ApiResult;
import com.ahui.mallchat.common.common.utils.AssertUtil;
import com.ahui.mallchat.common.common.utils.RequestHolder;
import com.ahui.mallchat.common.user.domain.dto.SummeryInfoDTO;
import com.ahui.mallchat.common.user.domain.enums.RoleEnum;
import com.ahui.mallchat.common.user.domain.vo.req.BlackReq;
import com.ahui.mallchat.common.user.domain.vo.req.ModifyNameReq;
import com.ahui.mallchat.common.user.domain.vo.req.SummeryInfoReq;
import com.ahui.mallchat.common.user.domain.vo.resp.BadgeResp;
import com.ahui.mallchat.common.user.domain.vo.resp.UserInfoResp;
import com.ahui.mallchat.common.user.service.RoleService;
import com.ahui.mallchat.common.user.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author ahui
 * @since 2024-07-20
 */
@RestController
@RequestMapping("/capi/user")
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private RoleService roleService;

    @GetMapping("/userInfo")
    @ApiOperation("获取用户个人信息")
    public ApiResult<UserInfoResp> getUserInfo() {
        return ApiResult.success(userService.getUserInfo(RequestHolder.get().getUid()));
    }

    @PutMapping("/name")
    @ApiOperation("修改用户名")
    public ApiResult<Void> modifyName(@Valid @RequestBody ModifyNameReq req) {//Valid注解校验请求参数
        userService.modifyName(RequestHolder.get().getUid(), req);
        return ApiResult.success();
    }

    @PutMapping("/black")
    @ApiOperation("拉黑用户")
    public ApiResult<List<BadgeResp>> black(@Valid @RequestBody BlackReq req){
        Long uid = RequestHolder.get().getUid();
        boolean hasPower = roleService.hasPower(uid, RoleEnum.ADMIN);
        AssertUtil.isTrue(hasPower,"抹茶管理员没有权限");
        userService.black(req);
        return ApiResult.success();
    }

    @PostMapping("/public/summary/userInfo/batch")
    @ApiOperation("用户聚合信息-返回的代表需要刷新的")
    public ApiResult<List<SummeryInfoDTO>> getSummeryUserInfo(@Valid @RequestBody SummeryInfoReq req) {
        return ApiResult.success(userService.getSummeryUserInfo(req));
    }


}

